package com.mk.admin.service.impl;

import com.mk.admin.mapper.NoticeLikeMapper;
import com.mk.admin.mapper.NoticeMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.NoticeLikeDto;
import com.mk.admin.model.entity.NoticeEntity;
import com.mk.admin.model.entity.NoticeLikeEntity;
import com.mk.admin.model.query.NoticeQuery;
import com.mk.admin.model.vo.NoticeVo;
import com.mk.admin.service.NoticeService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private NoticeLikeMapper noticeLikeMapper;

    @Override
    public ResultDto select(NoticeQuery vo) {
        NoticeEntity entity = JSONCopy.copy(vo, NoticeEntity.class);

        return ResultDto.data(noticeMapper.count(entity), noticeMapper.select(entity));
    }

    @Override
    public ResultDto add(NoticeVo vo) {
        vo.setCreateTime(DateTool.format());
        return ResultDto.data(noticeMapper.insert(JSONCopy.copy(vo, NoticeEntity.class)));
    }

    @Override
    public ResultDto update(NoticeVo vo) {

        return ResultDto.data(noticeMapper.updateById(JSONCopy.copy(vo, NoticeEntity.class)));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(noticeMapper.deleteById(id));
    }


    @Override
    public ResultDto status(int id) {
        return ResultDto.data(noticeMapper.status(id));
    }


    @Override
    public ResultDto selectById(int id) {
        return ResultDto.data(noticeMapper.selectById(id));
    }

    @Override
    public ResultDto deleteBatchIds(List<NoticeVo> list) {
        List<Integer> ids = list.stream().map(NoticeVo::getId).collect(Collectors.toList());
        return ResultDto.data(noticeMapper.deleteBatchIds(ids));
    }

    /**
     * 浏览次数，浏览后在notice_like表里面进行记录
     *
     * @param vo
     * @return
     */
    @Transactional
    @Override
    public ResultDto times(NoticeVo vo) {
        //        根据公告通知的id和用户patId查询是否有值，如果有，则直接更新浏览次数，如果没有，则创建一条数据，并更新

        NoticeLikeEntity entity = new NoticeLikeEntity();
        entity.setNoticeId(vo.getId());
        entity.setPatId(vo.getPatId());
        List<NoticeLikeDto> list = noticeLikeMapper.select(entity);
//        如果没有数据，则新增一条浏览数据，如果有，则不用任何操作
        if (list.size() == 0) {
            //        同时，需要对当前公告的浏览次数进行加1
            vo.setNum(1);
            noticeMapper.times(JSONCopy.copy(vo, NoticeEntity.class));
            entity.setStatusTimes(1);
            entity.setCreateTimeTimes(DateTool.format());
            noticeLikeMapper.insert(entity);
        }
        return ResultDto.data(noticeLikeMapper.select(entity));
    }

    /**
     * 点赞次数，点赞后在notice_like表里面进行记录
     *
     * @param vo
     * @return
     */
    @Transactional
    @Override
    public ResultDto likes(NoticeVo vo) {

        NoticeLikeEntity entity = new NoticeLikeEntity();
        entity.setNoticeId(vo.getId());
        entity.setPatId(vo.getPatId());
        noticeMapper.likes(JSONCopy.copy(vo, NoticeEntity.class));
        List<NoticeLikeDto> list = noticeLikeMapper.select(entity);
//        如果收藏表里面没有数据，则要新增数据，同时通知表要更新点赞
        if (list.size() == 0) {

            entity.setStatusLikes(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeLikes(DateTool.format());
            noticeLikeMapper.insert(entity);
        } else {
            entity.setId(list.get(0).getLikesId());
            entity.setStatusLikes(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeLikes(DateTool.format());
            noticeLikeMapper.updateById(entity);
        }

        return ResultDto.data("success");
    }

    @Transactional
    @Override
    public ResultDto collects(NoticeVo vo) {

        NoticeLikeEntity entity = new NoticeLikeEntity();
        entity.setNoticeId(vo.getId());
        entity.setPatId(vo.getPatId());
        noticeMapper.collects(JSONCopy.copy(vo, NoticeEntity.class));
        List<NoticeLikeDto> list = noticeLikeMapper.select(entity);
        if (list.size() == 0) {

            entity.setStatusCollects(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeCollects(DateTool.format());
            noticeLikeMapper.insert(entity);
        } else {
            entity.setId(list.get(0).getLikesId());
            entity.setStatusCollects(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeCollects(DateTool.format());
            noticeLikeMapper.updateById(entity);
        }

        return ResultDto.data("success");
    }

    @Transactional
    @Override
    public ResultDto forwards(NoticeVo vo) {

        NoticeLikeEntity entity = new NoticeLikeEntity();
        entity.setNoticeId(vo.getId());
        entity.setPatId(vo.getPatId());
        noticeMapper.forwards(JSONCopy.copy(vo, NoticeEntity.class));
        List<NoticeLikeDto> list = noticeLikeMapper.select(entity);
        if (list.size() == 0) {

            entity.setStatusForwards(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeForwards(DateTool.format());
            noticeLikeMapper.insert(entity);
        } else {
            entity.setId(list.get(0).getLikesId());
            entity.setStatusForwards(vo.getNum() == 1 ? 1 : 0);
            entity.setCreateTimeForwards(DateTool.format());
            noticeLikeMapper.updateById(entity);
        }

        return ResultDto.data("success");
    }


}
